home *** CD-ROM | disk | FTP | other *** search
- Path: solaris.cc.vt.edu!FQDN!usenet
- From: quartic@polloux.fciencias.unam.mx (Mena Quintero Federico)
- Newsgroups: comp.lang.c
- Subject: Re: Q: Newbie using text files
- Date: 18 Apr 1996 20:43:41 -0500
- Organization: Universidad Nacional Autonoma de Mexico
- Sender: quartic@polloux.fciencias.unam.mx
- Message-ID: <soenpl7zua.fsf@polloux.fciencias.unam.mx>
- References: <4l6aqu$rfr@chile.it.earthlink.net>
- Reply-To: quartic@polloux.fciencias.unam.mx
- NNTP-Posting-Host: polloux.fciencias.unam.mx
- In-reply-to: brunop@earthlink.net's message of Thu, 18 Apr 1996 20:04:46 GMT
- X-Newsreader: Gnus v5.1
-
- > use a betting langauage if I could. But, trying to input and
- > processes text files in C seems a lot harder than in C, what with
- > pointers and all.
-
- They say you can make your C programs as complex as you wish, so C
- being harder than C itself *could* be reasonable... ;)
-
- > My point, and I do have one, is it worth my time to learn C to process
- > ASCII text files with fixed lenth fields, for sorting etc...
-
- For one thing, your program will be much faster, and (if written
- properly) it will be portable. Anyway, if you *only* want to process
- text files, why not use Perl?
-
- > open "foo.bar" for input as #1
- > open "foo.out" for output as #2
- > while not eof(1)
- > line input #1, row
- > FName = mid$(row, 1, 25) 'characters 1-25 are first name
- > LName = mid$(row, 26, 50)
- > test = mid$(LName, 26, 1)
- > if test < "M" then print #2, row 'if person's last name begins with
- > character less then M print to output file, else we don't want them!
- > wend
- > close #1, #2
-
- Argh! BASIC in c.l.c!!! OK, so here it goes:
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define MAX_LEN 100
-
- int main(void)
- {
- FILE *input;
- FILE *output;
- char buf[MAX_LEN];
-
- input = fopen("foo.in", "rt");
- output = fopen("foo.out", "rt);
-
- if (!input || !output)
- exit(1);
-
- while (fgets(buf, MAX_LEN, input)
- if (buf[25] < 'M') /* It's always the 26th char, isn't it? */
- fputs(buf, output);
-
- fclose(input);
- fclose(output);
- } /* main */
-
-
- Hope this helps.
-
- Quartic
-